﻿using Db4objects.Db4o;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Northwind.Config;

namespace Northwind.Management.Customer
{
    /// <summary>
    /// Interaction logic for UserControl_Customer.xaml
    /// </summary>
    public partial class UserControl_Customer : UserControl, INotifyPropertyChanged
    {
        private bool _Update = false;

        #region Constructor
        public UserControl_Customer()
        {
            InitializeComponent();

            // Load Customer.
            using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
            {
                dataGrid1.ItemsSource = db.Query<Northwind.Objects.Customer>().ToList();
            }
            Customer = new Objects.Customer();
            this.DataContext = Customer;
        }
        #endregion Constructor End

        #region Properties
        private Northwind.Objects.Customer _Customer;
        public Northwind.Objects.Customer Customer
        {
            get
            { return _Customer; }
            set
            {
                _Customer = value;
                OnPropertyChanged("Customer");
            }
        }
        public IList<Northwind.Objects.Customer> Customers { get; set; }
        private IObjectContainer _DB4O;
        public IObjectContainer DB4O
        {
            get
            {
                if (_DB4O == null)
                    _DB4O = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName);
                return _DB4O;
            }
        }
        #endregion Properties End

        #region Methods
        private void LoadData()
        {
            try
            {
                //Customers = DB4O.Query<Northwind.Objects.Customer>();
                //dataGrid1.ItemsSource = Customers.ToList();
            }
            catch (Exception ex)
            {
            }
        }
        private string CreateCustomerID()
        {
            string result = string.Empty;
            string[] aS = Customer.CompanyName.Trim().Split(' ');
            foreach (var item in aS)
            {
                result += item.Substring(0,2);
            }
            aS = Customer.ContactName.Trim().Split(' ');
            foreach (var item in aS)
            {
                result += item.Substring(0,2);
            }

            return result;
        }
        private void Save()
        {
            try
            {
                Objects.Customer cus;
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    if (_Update)
                    {
                        cus = db.Query<Northwind.Objects.Customer>(p => p.CustomerID == Customer.CustomerID).FirstOrDefault();
                        if (cus != null)
                        {
                            CopyData(Customer, cus);
                            db.Store(cus);
                        }
                    }
                    else
                    {
                        Customer.CustomerID = CreateCustomerID();
                        db.Store(Customer);
                    }
                }
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    dataGrid1.ItemsSource = null;
                    dataGrid1.ItemsSource = db.Query<Northwind.Objects.Customer>().ToList();
                }
                cus = new Objects.Customer();
                CopyData(cus, Customer);
            }
            catch(Exception ex)
            {
            }
        }
        #endregion Methods End

        private void CopyData(Northwind.Objects.Customer pCustomerSrc, Northwind.Objects.Customer pCustomerDes)
        {
            pCustomerDes.Address = pCustomerSrc.Address;
            pCustomerDes.City = pCustomerSrc.City;
            pCustomerDes.CompanyName = pCustomerSrc.CompanyName;
            pCustomerDes.ContactName = pCustomerSrc.ContactName;
            pCustomerDes.ContactTitle = pCustomerSrc.ContactTitle;
            pCustomerDes.Country = pCustomerSrc.Country;
            pCustomerDes.CustomerID = pCustomerSrc.CustomerID;
            pCustomerDes.Fax = pCustomerSrc.Fax;
            pCustomerDes.Phone = pCustomerSrc.Phone;
            pCustomerDes.PostalCode = pCustomerSrc.PostalCode;
            pCustomerDes.Region = pCustomerSrc.Region;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Objects.Customer cus;
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    if (_Update)
                    {
                        cus = db.Query<Northwind.Objects.Customer>(p => p.CustomerID == Customer.CustomerID).FirstOrDefault();
                        if (cus != null)
                        {
                            CopyData(Customer, cus);
                            db.Store(cus);
                        }
                    }
                    else
                    {
                        Customer.CustomerID = CreateCustomerID();
                        db.Store(Customer);
                    }
                }
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    dataGrid1.ItemsSource = null;
                    dataGrid1.ItemsSource = db.Query<Northwind.Objects.Customer>().ToList();
                }
                cus = new Objects.Customer();
                CopyData(cus, Customer);
                _Update = false;
            }
            catch (Exception ex)
            {
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Objects.Customer cus;
                if (_Update)
                {
                    MessageBoxResult result = MessageBox.Show("Are you sure you want to remove this item ?", "Invoice", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {
                        using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                        {
                            cus = db.Query<Northwind.Objects.Customer>(p => p.CustomerID == Customer.CustomerID).FirstOrDefault();
                            if (cus != null)
                            {
                                db.Delete(cus);
                            }
                        }
                        using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                        {
                            dataGrid1.ItemsSource = null;
                            dataGrid1.ItemsSource = db.Query<Northwind.Objects.Customer>().ToList();
                        }
                        cus = new Objects.Customer();
                        CopyData(cus, Customer);
                        _Update = false;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dataGrid1.SelectedIndex = -1;
                Objects.Customer cus = new Objects.Customer();
                CopyData(cus, Customer);
                _Update = false;
            }
            catch (Exception ex)
            {
            }
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid1.CurrentItem != null)
            {
                Northwind.Objects.Customer cus = dataGrid1.CurrentItem as Northwind.Objects.Customer;
                CopyData(cus, Customer);
                _Update = true;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string pPropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(pPropertyName));
            }
        }
    }
}
